﻿using System;
using System.Data;
using System.Data.SqlClient;

namespace HRMService
{
    public partial class MainService : IServiceCharge
    {
        public DataSet ServiceCharge_GetOne(int ServiceChargeID)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_ServiceCharge_GetAll", dbConn);
            dbCmd.Parameters.AddWithValue("@ServiceCharge_ID", ServiceChargeID);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public DataSet ServiceCharge_GetList(out int ItemCount, int currentPage, int pageSize)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_ServiceCharge_GetList", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@CurrentPage", currentPage);
            dbCmd.Parameters.AddWithValue("@PageSize", pageSize);
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
                ItemCount = Convert.ToInt32(retVal.Tables[1].Rows[0][0]);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public ServiceChargeInfo ServiceCharge_GetInfo(int ServiceChargeID)
        {
            ServiceChargeInfo retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_ServiceCharge_GetInfo", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@ServiceCharge_ID", ServiceChargeID);
            SqlDataReader dr = null;
            try
            {
                dbConn.Open();
                dr = dbCmd.ExecuteReader();
                if (dr.Read())
                {
                    retVal = new ServiceChargeInfo();
                    retVal.ID = Convert.ToInt32(dr["ServiceCharge_ID"]);
                    retVal.Date = dr["ServiceCharge_Date"] == DBNull.Value ? DateTime.MinValue : Convert.ToDateTime(dr["ServiceCharge_Date"]);
                    retVal.TotalAmount = Convert.ToDecimal(dr["ServiceCharge_TotalAmount"]);
                    retVal.TotalEmployee = Convert.ToInt32(dr["ServiceCharge_TotalEmployee"]);
                    retVal.TotalWorkDays = Convert.ToDecimal(dr["ServiceCharge_TotalWorkDays"]);
                    retVal.TotalAbsentDays = Convert.ToDecimal(dr["ServiceCharge_TotalAbsentDays"]);
                    retVal.Description = dr["ServiceCharge_Description"].ToString();
                }
            }
            finally
            {
                if (dr != null) dr.Close();
                dbConn.Close();
            }
            return retVal;
        }

        public int ServiceCharge_Insert(ServiceChargeInfo Info)
        {
            int retVal = 0;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_ServiceCharge_Insert", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            if (Info.Date != DateTime.MinValue)
                dbCmd.Parameters.AddWithValue("@ServiceCharge_Date", Info.Date);
            else
                dbCmd.Parameters.AddWithValue("@ServiceCharge_Date", DBNull.Value);
            dbCmd.Parameters.AddWithValue("@ServiceCharge_TotalAbsentDays", Info.TotalAbsentDays);
            dbCmd.Parameters.AddWithValue("@ServiceCharge_TotalAmount", Info.TotalAmount);
            dbCmd.Parameters.AddWithValue("@ServiceCharge_TotalEmployee", Info.TotalEmployee);
            dbCmd.Parameters.AddWithValue("@ServiceCharge_TotalWorkDays", Info.TotalWorkDays);
            dbCmd.Parameters.AddWithValue("@ServiceCharge_Description", Info.Description);
            try
            {
                dbConn.Open();
                retVal = Convert.ToInt32(dbCmd.ExecuteScalar());
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public bool ServiceCharge_Update(ServiceChargeInfo Info)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_ServiceCharge_Update", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@ServiceCharge_ID", Info.ID);
            if (Info.Date != DateTime.MinValue)
                dbCmd.Parameters.AddWithValue("@ServiceCharge_Date", Info.Date);
            else
                dbCmd.Parameters.AddWithValue("@ServiceCharge_Date", DBNull.Value);
            dbCmd.Parameters.AddWithValue("@ServiceCharge_TotalAbsentDays", Info.TotalAbsentDays);
            dbCmd.Parameters.AddWithValue("@ServiceCharge_TotalAmount", Info.TotalAmount);
            dbCmd.Parameters.AddWithValue("@ServiceCharge_TotalEmployee", Info.TotalEmployee);
            dbCmd.Parameters.AddWithValue("@ServiceCharge_TotalWorkDays", Info.TotalWorkDays);
            dbCmd.Parameters.AddWithValue("@ServiceCharge_Description", Info.Description);
            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public bool ServiceCharge_Delete(int ServiceChargeID)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_ServiceCharge_Delete", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@ServiceCharge_ID", ServiceChargeID);
            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                    retVal = true;
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public bool ServiceCharge_Exist(int _ID, DateTime _Date)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_ServiceCharge_Exist", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@ServiceCharge_ID", _ID);
            dbCmd.Parameters.AddWithValue("@ServiceCharge_Date", _Date);
            try
            {
                dbConn.Open();
                if (Convert.ToInt32(dbCmd.ExecuteScalar()) > 0)

                    retVal = true;
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;

        }

        public bool ServiceCharge_Calculate(int ServiceChargeID, DateTime Date, Decimal Amount)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_ServiceCharge_Calculate", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@ServiceChargeID", ServiceChargeID);
            dbCmd.Parameters.AddWithValue("@Date", Date);
            dbCmd.Parameters.AddWithValue("@Amount", Amount);
            try
            {
                dbConn.Open();
                if (Convert.ToInt32(dbCmd.ExecuteScalar()) > 0)

                    retVal = true;
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;

        }

        public bool ServiceCharge_DeleteDetail(int ServiceChargeID)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_ServiceChargeDetail_Delete", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@ServiceCharge_ID", ServiceChargeID);
            
            try
            {
                dbConn.Open();
                if (Convert.ToInt32(dbCmd.ExecuteScalar()) > 0)

                    retVal = true;
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;

        }
    }
}
